package org.cjstudio.holypoopsgrs.model;

import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Order;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.cjstudio.holypoopsgrs_interface.game.model.HolyPoopsLeaderboardEntry;
import org.cjstudio.holypoopsgrs_interface.game.model.HolyPoopsPersonalRecord;
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class UserProfile {
	org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory a;

	@PrimaryKey	public String udid;

	//personal record
	@Persistent	public String name;
	@Persistent	public int highest;
	@Persistent	public int rank;
	@Persistent	public Date highestDate;

	//leaderboard entry
	@Persistent	public String message;
	@Persistent	public Date updateDate;
	@Persistent	public long numOfRecords;
	@Persistent	public double averageScore;

	@Persistent(mappedBy = "userProfile", defaultFetchGroup = "true")
	@Element(dependent = "true")
	@Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "score desc"))
	public List<UserRecord> topTenRecords;

	/*
	public void setUdid(String udid) {
		this.udid = udid;
	}*/
	public void setName(String name) {
		this.name = name;
	}
	public void setHighest(int highest) {
		this.highest = highest;
	}
	public void setRank(int rank) {
		this.rank = rank;
	}
	public void setHighestDate(Date highestDate) {
		this.highestDate = highestDate;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
	public void setNumOfRecords(long numOfRecords) {
		this.numOfRecords = numOfRecords;
	}
	public void setAverageScore(double averageScore) {
		this.averageScore = averageScore;
	}
	public void setTopTenRecords(List<UserRecord> records) {
		this.topTenRecords = records;
	}

	public HolyPoopsPersonalRecord toPersonalRecord() {
		HolyPoopsPersonalRecord record = new HolyPoopsPersonalRecord();
		record.name = this.name;
		record.highest = this.highest;
		record.rank = this.rank;
		record.highestDate = this.highestDate;
		return record;
	}

	public HolyPoopsLeaderboardEntry toEntry() {
		HolyPoopsLeaderboardEntry entry = new HolyPoopsLeaderboardEntry();
		entry.name = this.name;
		entry.highest = this.highest;
		entry.rank = this.rank;
		entry.highestDate = this.highestDate;
		entry.message = this.message;
		return entry;
	}

	@Override
	public String toString() {
		return "UserProfile [uuid=" + udid + ", name=" + name + ", highest="
		+ highest + ", rank=" + rank + ", highestDate=" + highestDate
		+ ", message=" + message + ", updateDate=" + updateDate
		+ ", numOfRecords=" + numOfRecords + ", averageScore="
		+ averageScore + ", topTenRecords=" + topTenRecords + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(averageScore);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + highest;
		result = prime * result
		+ ((highestDate == null) ? 0 : highestDate.hashCode());
		result = prime * result + ((message == null) ? 0 : message.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + (int) (numOfRecords ^ (numOfRecords >>> 32));
		result = prime * result + rank;
		result = prime * result
		+ ((topTenRecords == null) ? 0 : topTenRecords.hashCode());
		result = prime * result + ((udid == null) ? 0 : udid.hashCode());
		result = prime * result
		+ ((updateDate == null) ? 0 : updateDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserProfile other = (UserProfile) obj;
		if (Double.doubleToLongBits(averageScore) != Double
				.doubleToLongBits(other.averageScore))
			return false;
		if (highest != other.highest)
			return false;
		if (highestDate == null) {
			if (other.highestDate != null)
				return false;
		} else if (!highestDate.equals(other.highestDate))
			return false;
		if (message == null) {
			if (other.message != null)
				return false;
		} else if (!message.equals(other.message))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (numOfRecords != other.numOfRecords)
			return false;
		if (rank != other.rank)
			return false;
		if (topTenRecords == null) {
			if (other.topTenRecords != null)
				return false;
		} else if (!topTenRecords.equals(other.topTenRecords))
			return false;
		if (udid == null) {
			if (other.udid != null)
				return false;
		} else if (!udid.equals(other.udid))
			return false;
		if (updateDate == null) {
			if (other.updateDate != null)
				return false;
		} else if (!updateDate.equals(other.updateDate))
			return false;
		return true;
	}

}
